package com.todod.base.module.base.service.rtmaintain;

import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import com.todod.base.module.base.controller.admin.rtmaintain.vo.*;
import com.todod.base.module.base.dal.dataobject.rtmaintain.RtMaintainDO;
import com.todod.base.framework.common.pojo.PageResult;
import com.todod.base.framework.common.pojo.PageParam;
import com.todod.base.framework.common.util.object.BeanUtils;

import com.todod.base.module.base.dal.mysql.rtmaintain.RtMaintainMapper;

import static com.todod.base.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.todod.base.module.base.enums.ErrorCodeConstants.*;

/**
 * 实时数据维护 Service 实现类
 *
 * @author 超管儿
 */
@Service
@Validated
public class RtMaintainServiceImpl implements RtMaintainService {

    @Resource
    private RtMaintainMapper rtMaintainMapper;

    @Override
    public Long createRtMaintain(RtMaintainSaveReqVO createReqVO) {
        // 插入
        RtMaintainDO rtMaintain = BeanUtils.toBean(createReqVO, RtMaintainDO.class);
        rtMaintainMapper.insert(rtMaintain);
        // 返回
        return rtMaintain.getId();
    }

    @Override
    public void updateRtMaintain(RtMaintainSaveReqVO updateReqVO) {
        // 校验存在
        validateRtMaintainExists(updateReqVO.getId());
        // 更新
        RtMaintainDO updateObj = BeanUtils.toBean(updateReqVO, RtMaintainDO.class);
        rtMaintainMapper.updateById(updateObj);
    }

    @Override
    public void deleteRtMaintain(Long id) {
        // 校验存在
        validateRtMaintainExists(id);
        // 删除
        rtMaintainMapper.deleteById(id);
    }

    private void validateRtMaintainExists(Long id) {
        if (rtMaintainMapper.selectById(id) == null) {
            throw exception(RT_MAINTAIN_NOT_EXISTS);
        }
    }

    @Override
    public RtMaintainDO getRtMaintain(Long id) {
        return rtMaintainMapper.selectById(id);
    }

    @Override
    public PageResult<RtMaintainDO> getRtMaintainPage(RtMaintainPageReqVO pageReqVO) {
        return rtMaintainMapper.selectPage(pageReqVO);
    }

}